package com.wehotel.common.utils;


import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zqg
 */
public class AsynTaskPool {

    public static ExecutorService getExecutor(String name,int corePoolSize){
        return new ThreadPoolExecutor(corePoolSize, corePoolSize*2,0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(),
                new MyDefaultThreadFactory(name+"任务线程池"));

    }

    static class MyDefaultThreadFactory implements ThreadFactory {


        private final AtomicInteger threadNumber = new AtomicInteger(1);
        private final String namePrefix;

        public MyDefaultThreadFactory(String namePrefix) {

            this.namePrefix = namePrefix+"-";
        }

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread( r,
                    namePrefix + threadNumber.getAndIncrement()
            );
            t.setDaemon(true);
            return t;
        }
    }

}
